package com.pureut.storage.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pureut.storage.dto.PurchaseReceiptDetailDto;
import com.pureut.storage.mapper.PurchaseReceiptDetailMapper;
import com.pureut.storage.service.PurchaseReceiptDetailService;
import com.sale.common.core.constant.CacheNames;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.redis.utils.CacheUtils;
import com.sale.system.api.RemoteUnitService;
import com.sale.system.api.domain.SysAuxiliary;
import com.sale.system.api.domain.SysDictData;
import com.sale.system.api.model.MaterialUnitDto;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/***
 * 采购入库明细实现层
 * Author:C
 * Date:2023/01/17 11:11
 * @DESC
 */
@Service
public class PurchaseReceiptDetailServiceImpl implements PurchaseReceiptDetailService {

    @Resource
    PurchaseReceiptDetailMapper purchaseReceiptDetailMapper;

    @DubboReference
    RemoteUnitService remoteUnitService;


    /**
     * 采购入库明细列表
     * @param id
     * @return
     */
    @Override
    public TableDataInfo<PurchaseReceiptDetailDto> getReceiptDetail(long id, PageQuery pageQuery) {

        Page<PurchaseReceiptDetailDto> detailById = purchaseReceiptDetailMapper.getDetailById(pageQuery.build(), id);
        List<PurchaseReceiptDetailDto> records = detailById.getRecords();
        //状态
        List<SysDictData> statusArray = CacheUtils.get(CacheNames.SYS_DICT, "purchase_receipt_detail_status");
        Map<String, String> statusMap = statusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //单位集合
        Map<String, MaterialUnitDto> unitByCodeToMap = remoteUnitService.getUnitByCodeToMap();

        for (PurchaseReceiptDetailDto entity : records) {
            //辅助属性
            List<SysAuxiliary> statsArray = CacheUtils.get(CacheNames.SYS_GINSENG + "_" + entity.getTenantId(), String.valueOf(entity.getMaterialId()));
            entity.setAuxiliaryAttribute(statsArray);
            //状态
            entity.setStatusDict(statusMap.get(entity.getStatus()));
            //单位
            entity.setUnitDict(unitByCodeToMap.get(entity.getUnit()).getUnitName());
        }
        return TableDataInfo.build(detailById);
    }
}
